// pages/myRedPacket/myRedPacket.js
var util = require("../../utils/util.js")
var api = require("../../utils/api.js")
const app = getApp()

Page({

  /**
 
   * 页面的初始数据
   */
  data: {
    isLoad: true,
    navList: [{
      'select': true,
    }, {
      'select': false,
    }, {
      'select': false,
    }],
    myRedPacket: false,
    list: [{
      money: 150,
      startTime: '2018.04.06',
      endTime: '2019.05.20',
      use: false
    }, {
      money: 150,
      startTime: '2018.04.06',
      endTime: '2019.05.20',
      use: true
    }, {
      money: 150,
      startTime: '2018.04.06',
      endTime: '2019.05.20',
      use: true
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      width: wx.getSystemInfoSync().screenWidth,
      gid: options.gid || '',
      id: options.id || '',
    })
    let noData = {
      text: '没有数据'
    }
    this.getMyRedPacket(0)
  },
  getMyRedPacket: function(is_expire) {
    let data = {
      is_expire: is_expire
    }
    if (this.data.gid) {
      data.gid = this.data.gid
    }
    api.myRedPacket({
      data: data,
      success: res => {
        this.setData({
          red_packet: res.data.result.red_packet,
          expire_red_packet: res.data.result.expire_red_packet,
          expire_num: res.data.result.expire_num,
          no_expire_num: res.data.result.no_expire_num,
          used_num: res.data.result.used_num,
          used_red_packet: res.data.result.used_red_packet,
          isLoad: false,
          authStatus: app.globalData.authStatus
        })
      }
    })
  },
  useRedPacket: function (e) {
    let item = e.currentTarget.dataset.item
    if (item.status == '可使用') {
      if (this.data.gid) {
        //从商品详情页进来
        wx.navigateBack({
          delta: 1
        })
        wx.setStorageSync('red_packet', item)
      } else {
        // 从我的页面进来
        if (item.path && item.type == 2) {
          //针对单商品判断
          wx.navigateTo({
            url: `../commodityDetail/commodityDetail?gid=${item.path}&red_id=${item.id}`,
          })
        } else {
          wx.navigateTo({
            url: '../index/index',
          })
          // wx.navigateBack({
          //   delta: 1
          // })
        }
      }
    } else if (item.status == '已使用') {
      util.showToast('该红包抵扣券已使用~', 'none')
    } else if (item.status == '已过期') {
      util.showToast('该红包抵扣券已过期~', 'none')
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  // 顶部导航栏切换
  cutTop: function(e) {
    let index = e.currentTarget.dataset.index
    let navList = this.data.navList
    for (let i in navList) {
      if (i == index) {
        navList[i].select = true
      } else {
        navList[i].select = false
      }
    }
    this.setData({
      navList
    })
    let status
    if (index == 1) {
      status = 2
    }
    if (index == 2) {
      status = 1
    }
    this.getMyRedPacket(status)
  }
})